﻿using System.Collections.ObjectModel;
using System.Data;

using LightningTools.BCSTesterMan.Common.Infrastructure;

namespace LightningTools.BCSTesterMan.Model
{
    /// <summary>
    /// BCS Method
    /// </summary>
    public class Method : ObservableObject
    {
        private bool hasParameters;
        private bool hasResponse;
        private uint id;
        private bool isSupported;
        private MethodResponseDetail methodResponseDetail;
        private string methodType;
        private string name;
        private ObservableCollection<Parameter> parameters;
        private uint parentEntityId;

        /// <summary>
        /// Gets or sets a value indicating whether this instance is supported.
        /// </summary>
        /// <value>
        /// 	<c>true</c> if this instance is supported; otherwise, <c>false</c>.
        /// </value>
        public bool IsSupported
        {
            get { return isSupported; }
            set
            {
                isSupported = value;
                RaisePropertyChanged("IsSupported");
            }
        }

        private bool isLoading;
        public bool IsLoading
        {
            get { return isLoading; }
            set { isLoading = value; RaisePropertyChanged("IsLoading"); }
        }

        /// <summary>
        /// Gets or sets the name.
        /// </summary>
        /// <value>The name.</value>
        public string Name
        {
            get { return name; }
            set
            {
                name = value;
                RaisePropertyChanged("Name");
            }
        }

        /// <summary>
        /// Gets or sets the id.
        /// </summary>
        /// <value>The id.</value>
        public uint Id
        {
            get { return id; }
            set
            {
                id = value;
                RaisePropertyChanged("Id");
            }
        }

        /// <summary>
        /// Gets or sets the parent entity id.
        /// </summary>
        /// <value>The parent entity id.</value>
        public uint ParentEntityId
        {
            get { return parentEntityId; }
            set
            {
                parentEntityId = value;
                RaisePropertyChanged("ParentEntityId");
            }
        }

        /// <summary>
        /// Gets or sets the type of the method.
        /// </summary>
        /// <value>The type of the method.</value>
        public string MethodType
        {
            get { return methodType; }
            set
            {
                methodType = value;
                RaisePropertyChanged("MethodType");
            }
        }

        /// <summary>
        /// Gets or sets a value indicating whether this instance has parameters.
        /// </summary>
        /// <value>
        /// 	<c>true</c> if this instance has parameters; otherwise, <c>false</c>.
        /// </value>
        public bool HasParameters
        {
            get { return hasParameters; }
            set
            {
                hasParameters = value;
                RaisePropertyChanged("HasParameters");
            }
        }

        /// <summary>
        /// Gets or sets a value indicating whether this instance has response.
        /// </summary>
        /// <value>
        /// 	<c>true</c> if this instance has response; otherwise, <c>false</c>.
        /// </value>
        public bool HasResponse
        {
            get { return hasResponse; }
            set
            {
                hasResponse = value;
                RaisePropertyChanged("HasResponse");
            }
        }

        /// <summary>
        /// Gets or sets the parameters.
        /// </summary>
        /// <value>The parameters.</value>
        public ObservableCollection<Parameter> Parameters
        {
            get { return parameters; }
            set
            {
                parameters = value;

                if (parameters != null)
                {
                    HasParameters = parameters.Count > 0;
                }

                RaisePropertyChanged("Parameters");
            }
        }

        /// <summary>
        /// Gets or sets the method response detail.
        /// </summary>
        /// <value>The method response detail.</value>
        public MethodResponseDetail MethodResponseDetail
        {
            get { return methodResponseDetail; }
            set
            {
                methodResponseDetail = value;

                HasResponse = methodResponseDetail != null;

                RaisePropertyChanged("MethodResponseDetail");
            }
        }

        /// <summary>
        /// Returns a <see cref="System.String"/> that represents this instance.
        /// </summary>
        /// <returns>
        /// A <see cref="System.String"/> that represents this instance.
        /// </returns>
        public override string ToString()
        {
            return Name;
        }
    }

    public class MethodResponseDetail : ObservableObject
    {
        private string detail;
        private DataView methodDataView;

        /// <summary>
        /// Initializes a new instance of the <see cref="MethodResponseDetail"/> class.
        /// </summary>
        /// <param name="detail">The detail.</param>
        /// <param name="methodDataView">The method data view.</param>
        public MethodResponseDetail(string detail, DataView methodDataView)
        {
            Detail = detail;
            MethodDataView = methodDataView;
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="MethodResponseDetail"/> class.
        /// </summary>
        /// <param name="detail">The detail.</param>
        /// <param name="dataTable">The data table.</param>
        public MethodResponseDetail(string detail, DataTable dataTable)
        {
            Detail = detail;
            MethodDataView = dataTable.AsDataView();
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="MethodResponseDetail"/> class.
        /// </summary>
        /// <param name="detail">The detail.</param>
        public MethodResponseDetail(string detail)
        {
            Detail = detail;
            MethodDataView = null;
        }

        /// <summary>
        /// Gets or sets the detail.
        /// </summary>
        /// <value>The detail.</value>
        public string Detail
        {
            get { return detail; }
            set
            {
                detail = value;
                RaisePropertyChanged("Detail");
            }
        }

        /// <summary>
        /// Gets or sets the method data view.
        /// </summary>
        /// <value>The method data view.</value>
        public DataView MethodDataView
        {
            get { return methodDataView; }
            set
            {
                methodDataView = value;
                RaisePropertyChanged("MethodDataView");
            }
        }
    }
}